<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/15 0015
 * Time: 上午 9:34
 */

namespace Home\Model;
use Think\Model;

class BCZdysxModel extends Model{

    /**
     * 根据属性筛选
     * @param $sx 提交的筛选属性值
     * @param $value  flno or cpxlno
     * @param $type   分类 or  系列
     * @return array
     */
    public function filter_sx($sx,$value,$type){
        $a = microtime(true);
        $sx_count = count($sx);
        $sxnos = implode(',',array_keys($sx));
        if($type == 'wzfl'){
            $wzno_sql = "select a.wzno from b_c_zdysx a 
                         left join b_c_wzbm b on a.wzno=b.wzno where b.wzfl={$value} group by a.wzno";

            $sxz_sql = "select b.wzno ,group_concat(b.zdysxz) sxz from b_c_wzbm a
                        join b_c_zdysx b on a.wzno=b.wzno
                        where a.wzfl={$value} and b.sxno in ($sxnos)
                        group by b.wzno ";
        }else if($type == 'cpxl'){
            $wzno_sql = "select a.wzno from b_c_wzbm a 
                         left join b_p_cpxlwzbm b on a.wzno=b.wzno where b.cpxlno={$value}";

            $sxz_sql = "select a.wzno ,group_concat(b.zdysxz) sxz from b_p_cpxlwzbm a
                        join b_c_zdysx b on a.wzno=b.wzno
                        where a.cpxlno={$value} and b.sxno in ({$sxnos})
                        group by a.wzno";
        }
        $wznos = D()->query($wzno_sql);
        //dump($wznos);
        $info = D()->query($sxz_sql);
        //dump($info);exit;

        $str = '';
        foreach ($sx as $k => $v) {
            foreach ($v as $k1 => $v1) {
                foreach ($info as $k2 => $v2) {
                    $count = substr_count($v2['sxz'], $v1);
                    if ($count > 0) {
                        $str .= $v2['wzno'] . ',';
                    }
                }
            }
        }
        //dump($str);exit;
        $data = [];
        foreach ($wznos as $k => $v) {
            if ($sx_count == substr_count($str, $v['wzno'])) {
                $data[] = $v;
            }
        }
        //dump($data);exit;

        $data = array_column($data,'wzno');
        //dump(microtime(true)-$a);
        return $data ;
    }

}